package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public PageResult queryPage(DishPageQueryDTO dishPageQueryDTO) {
        //设置分页参数
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.queryPage(dishPageQueryDTO);
        PageResult pageResult = new PageResult();
        pageResult.setRecords(page.getResult());
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    @Override
    @Transactional
    public void addDishWithFalvor(DishDTO dishDTO) {
// TODO向菜品表插入一条数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insertDish(dish);
        Long insertedId = dish.getId();

        //向口味表插入数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            flavors.forEach(flavor ->{
                flavor.setDishId(insertedId);
            });
            dishFlavorMapper.insertFlavors(flavors);
        }
    }

    @Override
    @Transactional
    public void deleteDishes(Long[] ids) {
        //判断看ids中有没有status  = 1的菜品
        Integer count = dishMapper.countEnableDishes(ids);
        if(count > 0){
            throw new RuntimeException("存在启用中的菜品，无法删除");
        }
        //判断有没有被套餐关联的菜品id
        Integer countByDishIds = setmealDishMapper.countByDishIds(ids);
        if(countByDishIds > 0){
            throw new RuntimeException("存在被套餐关联的菜品，无法删除");
        }

        //先删除菜品口味表
        dishFlavorMapper.deleteByDishIds(ids);
        //再删除菜品表
        dishMapper.deleteDishes(ids);
    }

    @Override
    public DishVO getDishVO(Long id) {
        Dish dish = dishMapper.getDishById(id);
        List<DishFlavor> dishFlavor = dishFlavorMapper.getFlavorsByDishId(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }

    @Override
    @Transactional
    public void updateDishAndFalvors(DishVO dishVO) {
        //更新菜品表
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishVO, dish);
        dishMapper.updateDish(dish);

        //删除原来的口味
        dishFlavorMapper.deleteByDishIds(new Long[]{dishVO.getId()});

        //插入新的口味
        List<DishFlavor> flavors = dishVO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            flavors.forEach(flavor ->{
                flavor.setDishId(dishVO.getId());
            });
            dishFlavorMapper.insertFlavors(flavors);
        }
    }

    @Override
    public List<Dish> querydishlistByCategoryId(Long categoryId) {
        List<Dish> dishList = dishMapper.querydishlistByCategoryId(categoryId);

        return dishList;
    }
}
